/**
 * Project: LiangDev
 * 
 * File Created at 2012-9-22
 * $Id$
 * 
 * Copyright 1999-2100 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.liangdev.biz.entity.autocode;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.liangdev.common.util.SetUtil;
import com.liangdev.common.util.StringUtil;

/**
 * 表Meta信息
 * 
 * @author chongan.wangca
 */
public class TableMetaData {

    /* 数据库类型，ORACLE/MYSQL */
    private String              dbType     = "ORACLE";

    /* 是否需要处理前缀，默认为需要处理 */
    private boolean             dealPrefix = true;

    /* 数据库表名，比如：wl_trade */
    private String              dbTableName;

    /* 字段信息 */
    private List<FieldMetaData> fieldMetaDatas;

    /**
     * 获取所有数据库的字段的名称拼接成的串
     * 
     * @return
     */
    public String getAllDbFieldName() {
        if (fieldMetaDatas == null || fieldMetaDatas.size() == 0) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < fieldMetaDatas.size(); i++) {
            FieldMetaData fieldMetaData = fieldMetaDatas.get(i);
            sb.append(fieldMetaData.getDbFieldNameUC());
            if (i < fieldMetaDatas.size() - 1) {
                sb.append(",");
                sb.append("<br/>");
            }
        }
        return sb.toString();
    }

    /**
     * 获取所有数据库的字段的名称拼接成的串
     * 
     * @return
     */
    public String getDbFieldNameExclude(String... excludeFields) {
        if (fieldMetaDatas == null || fieldMetaDatas.size() == 0) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < fieldMetaDatas.size(); i++) {
            FieldMetaData fieldMetaData = fieldMetaDatas.get(i);

            if (excludeFields != null) {
                boolean isExclude = isExcludeField(fieldMetaData, excludeFields);
                if (isExclude)
                    continue;
            }

            sb.append(fieldMetaData.getDbFieldNameUC());
            if (i < fieldMetaDatas.size() - 1) {
                sb.append(",");
                sb.append("<br/>");
            }
        }
        return sb.toString();
    }

    /**
     * 获取所有字段的名称拼接成的串
     * 
     * @return
     */
    public String getAllFieldNameSha() {
        if (fieldMetaDatas == null || fieldMetaDatas.size() == 0) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < fieldMetaDatas.size(); i++) {
            FieldMetaData fieldMetaData = fieldMetaDatas.get(i);
            sb.append("#" + fieldMetaData.getFieldNameLF() + "#");
            if (i < fieldMetaDatas.size() - 1) {
                sb.append(",");
                sb.append("<br/>");
            }
        }
        return sb.toString();
    }

    /**
     * 获取所有字段的名称拼接成的串,排除指定的字段
     * 
     * @return
     */
    public String getFieldNameShaExclude(String... excludeFields) {
        if (fieldMetaDatas == null || fieldMetaDatas.size() == 0) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < fieldMetaDatas.size(); i++) {

            FieldMetaData fieldMetaData = fieldMetaDatas.get(i);

            if (excludeFields != null) {
                boolean isExclude = isExcludeField(fieldMetaData, excludeFields);
                if (isExclude)
                    continue;
            }

            sb.append("#" + fieldMetaData.getFieldNameLF() + "#");
            if (i < fieldMetaDatas.size() - 1) {
                sb.append(",");
                sb.append("<br/>");
            }
        }
        return sb.toString();
    }

    /**
     * @param fieldMetaData
     * @param excludeFields
     */
    private boolean isExcludeField(FieldMetaData fieldMetaData, String... excludeFields) {
        for (String field : excludeFields) {
            if (fieldMetaData.getDbFieldNameUC().equals(field.toUpperCase())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取当前时间的SQL语句
     * 
     * @return
     */
    public String getCurrentTimeSql() {
        if ("ORACLE".endsWith(dbType)) {
            return "sysdate";
        }
        if ("MYSQL".endsWith(dbType)) {
            return "now()";
        }
        return null;
    }

    public boolean isDealPrefix() {
        return dealPrefix;
    }

    public void setDealPrefix(boolean dealPrefix) {
        this.dealPrefix = dealPrefix;
    }

    public void setDbTableName(String dbTableName) {
        this.dbTableName = dbTableName;
    }

    /**
     * 获取剔除掉参数中的字段
     * 
     * @param fieldNames 可以传多个，字段名为java字段名，或者数据库字段名，不区分大小写
     * @return
     */
    public List<FieldMetaData> getExcludeFieldMetaDatas(String... fieldNames) {

        if (this.fieldMetaDatas == null || fieldMetaDatas.size() == 0 || fieldNames == null
                || fieldNames.length == 0) {
            return this.fieldMetaDatas;
        }

        Set<String> excludeFieldSet = SetUtil.string2SetUpperCase(fieldNames);
        List<FieldMetaData> newFieldMetaDatas = new ArrayList<FieldMetaData>(this.fieldMetaDatas);
        Iterator<FieldMetaData> it = newFieldMetaDatas.iterator();

        while (it.hasNext()) {
            FieldMetaData data = it.next();
            if (excludeFieldSet.contains(data.getFieldNameLF().toUpperCase())
                    || excludeFieldSet.contains(data.getDbFieldNameUC().toUpperCase())) {
                it.remove();
            }
        }

        return newFieldMetaDatas;
    }

    public String getDbType() {
        return dbType;
    }

    public void setDbType(String dbType) {
        this.dbType = dbType;
    }

    /**
     * 表名，会自动把下横线_转化为大写，即驼峰形式，首字母小写，不含前缀。比如：trade
     * 
     * @return
     */
    public String getTableNameLF() {

        //如果有前缀，则先去掉前缀
        String prefix = getPrefix();
        String newTableName = dbTableName.toLowerCase();
        if (prefix != null) {
            newTableName = StringUtil.removePrefix(newTableName, prefix + "_", true);
        }

        newTableName = StringUtil.firstUpperCaseAll(newTableName, "_");
        newTableName = StringUtil.firstLetterLower(newTableName);

        return newTableName;
    }

    /**
     * 表名，会自动把下横线_转化为大写，即驼峰形式，首字母大写，不含前缀。比如：Trade
     * 
     * @return
     */
    public String getTableNameUF() {
        return StringUtil.firstLetterUpper(getTableNameLF());
    }

    /**
     * 表名前缀。第一个下横线_前面的部分(不含_)计为前缀。全部为大写。如果没有_，则 为字段为NULL。比如：WL
     * 
     * @return
     */
    public String getPrefix() {

        if (this.dealPrefix == false || this.dbTableName.indexOf("_") == 0) {
            return null;
        }

        return this.dbTableName.substring(0, this.dbTableName.indexOf("_")).toUpperCase();
    }

    /**
     * 完整原始表名，会自动把下横线_转化为大写，即驼峰形式，首字母小写，含前缀。比如：wlTrade
     * 
     * @return
     */
    public String getTableNameFullLF() {

        String newTableName = dbTableName.toLowerCase();
        newTableName = StringUtil.firstUpperCaseAll(newTableName, "_");
        newTableName = StringUtil.firstLetterLower(newTableName);
        return newTableName;

    }

    /**
     * 表名，会自动把下横线_转化为大写，即驼峰形式，首字母大写，含前缀。比如：WlTrade
     * 
     * @return
     */
    public String getTableNameFullUF() {
        return StringUtil.firstLetterUpper(getTableNameFullLF());
    }

    /**
     * 获取所有字段信息
     * 
     * @return
     */
    public List<FieldMetaData> getFieldMetaDatas() {
        return fieldMetaDatas;
    }

    public void setFieldMetaDatas(List<FieldMetaData> fieldMetaDatas) {
        this.fieldMetaDatas = fieldMetaDatas;
    }

    /**
     * 获取字段个数
     * 
     * @return
     */
    public Integer getFieldCount() {
        if (this.fieldMetaDatas == null || this.fieldMetaDatas.size() == 0) {
            return 0;
        }
        return this.fieldMetaDatas.size();
    }

    public String getDbTableName() {
        return dbTableName;
    }

    public String getDbTableNameUC() {
        return dbTableName.toUpperCase();
    }

}
